﻿using Lazy.Captcha.Core.Storage.Caches;
using System;
using System.Collections.Generic;
using System.Text;

namespace Lazy.Captcha.Core.Storage
{
    public class MemoryStorage : IStorage
    {
        private readonly MemoryCache _cache = MemoryCache.Default;
        public string StorageKeyPrefix { get; set; } = string.Empty;

        [Obsolete("由于拼写错误，已被废弃。请使用 `StorageKeyPrefix`。", false)]
        public string StoreageKeyPrefix
        {
            get => StorageKeyPrefix;
            set => StorageKeyPrefix = value;
        }

        private string WrapKey(string key)
        {
            return $"{StorageKeyPrefix}{key}";
        }

        public string Get(string key)
        {
            return _cache.Get(WrapKey(key));
        }

        public void Remove(string key)
        {
            _cache.Remove(WrapKey(key));
        }

        public void Set(string key, string value, DateTimeOffset absoluteExpiration)
        {
            _cache.Set(WrapKey(key), value, absoluteExpiration);
        }
    }
}
